#include "accountprofileresponse.h"
#include <QDateTime>
#include <QVariant>
#include <QDebug>


namespace Account {

ProfileResponse::ProfileResponse(QObject *parent):
    JSONResponse(parent)
{
}

Network::Response::Type ProfileResponse::type() const
{
    return Response::ProfileType;
}

void ProfileResponse::parse(const QVariant &data)
{
    //qDebug() << "Parsr Data Profile Response:";
    //qDebug() << data;
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The error code field isn't competitived");
        return;
    }

    QVariantMap map = data.toMap();

    if(!map.contains("list")){
        setError(Response::Property, "The \"list\" value missing");
        return;
    }

    QVariant list = map.value("list");
    if(list.type() != QVariant::List){
        setError(Response::Property, "The error code field isn't competitived");
        return;
    }
    QVariantList List = list.toList();
    foreach (QVariant item, List) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The \"map\" field isn't a map"));
            return;
        }

        QVariantMap map = item.toMap();
        if (!map.contains("uid")) {
            setError(Response::Property, "The \"uid\" value missing");
            return;
        }

        QVariant userId = map.value("uid");
        if (userId.type() != QVariant::Int) {
            setError(Response::Property, "The \"uid\" value isn't competitived");
            return;
        }

        QVariant email = map.value("eml");
        QVariant displayName = map.value("dpn");
        QVariant profile_url = map.value("url");
        QVariant userName = map.value("urn");
        QVariant mobile = map.value("mob");
        QVariant avatar = map.value("avt");
        QVariant gender = map.value("ged");
        QVariant firstName = map.value("fin");
        QVariant lastName = map.value("lan");
        QVariant googleID = map.value("gid");
        QVariant yahooId = map.value("yid");
        QVariant city = map.value("cit");
        QVariant birthday = map.value("dob");


        m_userId = userId.toInt();
        m_email = email.toString();
        m_displayName = displayName.toString();
        m_userName = userName.toString();
        m_profileUrl = profile_url.toString() ;
        m_avatar =  avatar.toString();
        m_gender = gender.toInt();
        m_firstName = firstName.toString();
        m_lastName = lastName.toString();
        m_googleId = googleID.toString();
        m_yahooID = yahooId.toString();
        m_city = city.toString();
        m_birthday = QDateTime(birthday.toDateTime()).toString("yyyy-MM-dd");

        QVariantList listMobile = mobile.toList();
        foreach (QVariant item, listMobile) {
            m_mobi.append(item.toString());
        }

    }
}

}
